﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EmissionFlare {

    Color m_emissColor = Color.red;

    Material[] m_mat;
    public bool m_isFlare = false;

    // Use this for initialization
    void Start() {

    }

    public void MakeFlare( bool isFlare, MeshRenderer meshRender ) {
        m_isFlare = isFlare;
        m_mat = meshRender.materials;
        EnableFlare( m_isFlare );
    }

    // Update is called once per frame
    public void Update() {

        FlareRed();
    }

    void EnableFlare( bool beEnable ) {
        if (beEnable)
            foreach (var mat in m_mat) {
                mat.EnableKeyword( "_EMISSION" );
            }
        else {
            foreach (var mat in m_mat) {
                mat.DisableKeyword( "_EMISSION" );
            }
        }
    }

    void FlareRed() {
        if (m_isFlare) {
            foreach (var mat in m_mat) {
                m_emissColor.r = Mathf.Abs( Mathf.Sin( Time.timeSinceLevelLoad *2.0f) );
                mat.SetColor( "_EmissionColor", m_emissColor );
            }
        }
    }
}
